// pages/newProduct/newProduct.js
var utils = require('../../utils/util.js');
var base64Js = require('../../utils/base64.js');
var md5Js = require('../../utils/md5.js');
var app = getApp();
Page({
  data:{
      activeIndex: 0,
      actionSheetHidden: true,
      sku_info_checkout:[],
      index:0,
      num:1,
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
     try{
         var _this = this;
        //获取缓存的用户对象信息
        var user_info =  wx.getStorageSync('user_info');
        //如果用户信息不为空则请求首页接口获取产品等信息。
        if(user_info){
          var request_data = user_info.user_itmes;
          var uid         = request_data.id.toString();
          var private_key = user_info.private_key;
          var t           =  new Date().getTime().toString();
          var k           = md5Js.hex_md5(private_key+t);//key
          var parms_url   = app.request_url+'C_NewProduct/?';
          parms_url += 'enterpriseId='+request_data.enterpriseId;
          parms_url += '&uid='+request_data.id;
          parms_url += '&channelId='+request_data.channelId;
          parms_url += '&userType='+request_data.type;
          parms_url += '&t='+t;
          parms_url += '&k='+k;
          parms_url += '&v='+app.version;;
          // parms_url = 'http://rest.test.freshqiao.com/C_NewProduct/?uid=100000202&channelId=204&userType=1&enterpriseId=79&t=1486950553953&k=a7577e7a0024b4662b79113e6a82c62d&uid=100000202&v=2';
          //调用产品页接口
          wx.request({
            url: parms_url,
            method: 'GET',
            success: function(res){
               var respones = res;
               //请求成功分配模板变量
               if((respones.statusCode=='200')&&(respones.data.Success==1)){
                   //分配模板变量
                   var data = respones.data.CallInfo;
                   //分配模板变量
                   var data_product = data.product_list;
                   var product_length = data_product.length;
                   for(var i=0;i<product_length;i++){
                      var price = data_product[i].sku_info[0].sellprice;
                      var scell_price = 0
                      if(price){
                          scell_price = price;
                      }
                      data_product[i].sku_info_string = JSON.stringify(data_product[i].sku_info);
                      data_product[i].scell_price_string = scell_price;
                   }
                   var cart_info =  wx.getStorageSync('cart_info');
                   console.log(data_product);
                   //分配模板变量
                   _this.setData({
                       imgUrls:data.advertise_list,
                       product_list:data_product,
                       cart_num:cart_info.length
                   });
               }
            }
          })
        }
    }catch(e){
      console.log('error=>'+e.message);return false;
    }
  },

  bindMinus: function(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var num = this.data.num;
    // 如果只有1件了，就不允许再减了
    if (num > 1) {
      num --;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 按钮可用状态
    var minusStatuses = this.data.minusStatuses;
    // 将数值与状态写回
    this.setData({
      num: num,
      minusStatuses: minusStatus
    });
  },
  bindPlus: function(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var num = this.data.num;
    // 自增
    num ++;
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 按钮可用状态
    var minusStatuses = this.data.minusStatuses;
    // 将数值与状态写回
    this.setData({
      num: num,
      minusStatuses: minusStatus
    });
  },

  showModal: function (e) {
    // 显示遮罩层
    var sku_info = JSON.parse(e.currentTarget.dataset.info);console.log(sku_info);
    var product_name = e.currentTarget.dataset.productname;console.log(product_name);
    var productinfo = e.currentTarget.dataset.productinfo;console.log(productinfo);
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      product_name:product_name,
      sku_info:sku_info,
      product_info:productinfo,
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

//选择属性
 radioChange: function (e) {
    this.setData({
      sku_info_checkout: e.currentTarget.dataset.skuinfo,
      activeIndex: e.currentTarget.dataset.index
    });
  },
//进入详情页
product_detail:function(e){
  var id = e.currentTarget.dataset.id;
  console.log(id);
     wx.navigateTo({
      url: '../shopdetail/shopdetail?id='+id,
    })
},

  //点击购物车图片时添加商品
  submit: function(e){
    var cart = require('../../utils/cart.js');
    cart.submitcart(e, this)
},

  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})